import java.util.Comparator;
import java.util.PriorityQueue;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Admin
 * Date: 2022-05-24
 * Time: 10:40
 */
public class TestDemo {
    public int[] smallestK(int[] arr, int k) {
        if( k == 0) {
            return new int[k];

        }

        PriorityQueue<Integer> PriorityQueue = new PriorityQueue<>(k,new Comparator<Integer>(){
            public int compare(Integer o1,Integer o2) {
                return o2 - o1;
            }
        });

        for(int i = 0;i < arr.length;i++) {
            if(i < k) {
                PriorityQueue.offer(arr[i]);
            } else {
                if(arr[i] < PriorityQueue.peek()) {
                    PriorityQueue.poll();
                    PriorityQueue.offer(arr[i]);
                }
            }
        }
        int[] ret = new int[k];
        for(int i = 0;i < k;i++) {
            ret[i] = PriorityQueue.poll();
        }

        return ret;
    }
    public static void main(String[] args) {

    }
}
